/**
 * Created by taohj on 2014/4/21.
 */



var functionMenu = null;

var functions = new util.Map();

var main = null;

$(init);

function init() {


    functionMenu = $("#functionMenu");
    main = $("#main");
    main.html("<div></div>");
    main = main.find("div");


    //显示标签页
    main.tabs({
        fit: true,
        border: false,
        plain: true,
        onContextMenu: function (e, title, index) {
            e.preventDefault();
            var contextMenu = $('<div></div>');
            contextMenu.menu({});
            contextMenu.menu('appendItem', {
                text: 'Close Current Tab',
                onclick: function () {
                    main.tabs("close", title);
                }
            });

            contextMenu.menu('appendItem', {
                text: 'Close All Tabs',
                onclick: function () {
                    var tabs = main.tabs("tabs");
                    while (tabs && tabs.length > 0) {
                        main.tabs("close", 0);
                    }
                }
            });


            contextMenu.menu('show', {
                left: e.pageX,
                top: e.pageY
            });
        }
    });


    //console.info(main);

    /*
     * 构建功能菜单
     * */
    functionMenu.tree({
        url: 'getFunctinMenuTree',
        method: 'post',
        onClick: function (node) {

            if (node.type == '1' && node.url && node.url.length > 0) {

                if (!main.tabs('exists', node.text)) {
                    main.tabs('add', {
                        title: node.text,
                        href: node.url,
                        closable: true,
                        fit: true
                    });
                } else {
                    main.tabs('select', node.text);
                }

                // util.createTab(main,node);
            }

        }
    });




    //初始化主题
    var subject = $("#subject");
    subject.combobox({
        valueField: 'value',
        textField: 'text',
        groupField: 'group',
        data: [
            {
                value: "default",
                text: "Default",
                group: "Base"
            }, {
                value: "gray",
                text: "Gray",
                group: "Base"
            }, {
                value: "metro",
                text: "Metro",
                group: "Base"
            }, {
                value: "bootstrap",
                text: "Bootstrap",
                group: "Base"
            }, {
                value: "black",
                text: "Black",
                group: "Base"
            }, {
                value: "metro-blue",
                text: "Metro Blue",
                group: "Metro"
            }, {
                value: "metro-gray",
                text: "Metro Gray",
                group: "Metro"
            }, {
                value: "metro-green",
                text: "Metro Green",
                group: "Metro"
            }, {
                value: "metro-orange",
                text: "Metro Orange",
                group: "Metro"
            }, {
                value: "metro-red",
                text: "Metro Red",
                group: "Metro"
            }, {
                value: "ui-cupertino",
                text: "Cupertino",
                group: "UI"
            }, {
                value: "ui-dark-hive",
                text: "Dark Hive",
                group: "UI"
            }, {
                value: "ui-pepper-grinder",
                text: "Pepper Grinder",
                group: "UI"
            }, {
                value: "ui-sunny",
                text: "Sunny",
                group: "UI"
            }
        ],
        onSelect: function (record) {
            var href = "resources/js/lib/jquery-easyui-1.4.1/themes/" + record.group.toLowerCase() + "/" + record.value + "/easyui.css";
            $.cookie("subject_href", href);
            $.cookie("subject_value", record.value);

            var subject_link = $("#subject_link");
            subject_link.attr("href", href);
        }
    });


    /**
    * 设置主题
    * */
    subject.combobox("setValue", $.cookie("subject_value"));
    var subject_link = $("#subject_link");
    subject_link.attr("href", $.cookie("subject_href"));



    /**
    *屏蔽掉Backspace 返回
    * */
    document.onkeydown=function() {
        if (event.srcElement.type != 'text' && event.srcElement.type != 'textarea')
            if (event.keyCode == 8) {
                event.keyCode = 0;
                event.returnValue = false;
            }
    };

    initFuntions();
    loadJss();
}


function loadJss() {
    var path = "resources/js/thomas/sys/";
    util.loaddJS(path + "model.js");
    util.loaddJS(path + "role.js");
    util.loaddJS(path + "user.js");
    util.loaddJS(path + "dictionary/dictionaryGroup.js");
    util.loaddJS(path + "dictionary/dictionary.js");
}


function initFuntions() {
    $.post('getFunctinMenu', {}, function (result) {
        var data = $.parseJSON(result);

        functions = new util.Map();

        for (var item in data) {
            if (typeof(data[item]) != "function") {
                item.toString = function () {
                    return this.url;
                }
                functions.put(item, data[item].url);
            }
        }

    });

}


